package com.example.seckilldemo.rabbitmq;

import com.example.seckilldemo.mapper.OrderMapper;
import com.example.seckilldemo.pojo.SeckillMessage;
import com.example.seckilldemo.pojo.SeckillOrder;
import com.example.seckilldemo.pojo.User;
import com.example.seckilldemo.service.IGoodsService;
import com.example.seckilldemo.service.IOrderService;
import com.example.seckilldemo.utils.JsonUtil;
import com.example.seckilldemo.vo.GoodsVo;
import com.example.seckilldemo.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author Wangbaba
 * @Date 2024/3/20 23:01
 * @Version 1.0
 * 消息消费者
 */
@Service
@Slf4j
public class MQReceiver {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IOrderService orderService;

    /**
     * 下单操作
     */
    @RabbitListener(queues = "seckillQueue")
    public void receive(String msg){
        log.info("接收的消息:"+msg);
        SeckillMessage seckillMessage= JsonUtil.jsonStr2Object(msg,SeckillMessage.class);
        GoodsVo goodsVo=goodsService.findGoodsById(seckillMessage.getGoodsId());
        if (goodsVo.getStockCount()<1){
            return;
        }
        //判断是否重复抢购
        SeckillOrder seckillOrder= (SeckillOrder) redisTemplate.opsForValue().get("order:"+seckillMessage.getUser().getId()+";"+goodsVo.getId());
        if (Objects.nonNull(seckillOrder)) {
           return;
        }
        //下单
        orderService.seckill(seckillMessage.getUser(),goodsVo);
    }

    /**
     * MQ测试 消息接收
     * @param msg
     */
//    @RabbitListener(queues = "queue")
//    public void receiver(Object msg){
//        log.info("接收消息:"+msg);
//    }
//
//    @RabbitListener(queues = "queue_fanout01")
//    public void receiver01(Object msg){
//        log.info("queue01接收消息:"+msg);
//    }
//    @RabbitListener(queues = "queue_fanout02")
//    public void receiver02(Object msg){
//        log.info("queue02接收消息:"+msg);
//    }
//
//    @RabbitListener(queues = "queue_direct01")
//    public void receiver03(Object msg){
//        log.info("queue01接收消息:"+msg);
//    }
//
//    @RabbitListener(queues = "queue_direct02")
//    public void receiver04(Object msg){
//        log.info("queue02接收消息:"+msg);
//    }
//    @RabbitListener(queues = "queue_topic01")
//    public void receiver05(Object msg){
//        log.info("queue01接收消息:"+msg);
//    }
//    @RabbitListener(queues = "queue_topic02")
//    public void receiver06(Object msg){
//        log.info("queue02接收消息:"+msg);
//    }
//
//    @RabbitListener(queues = "queue_header01")
//    public void receiver07(Message msg){
//        log.info("queue01接收对象:"+msg);
//        log.info("queue01接收消息:"+new String(msg.getBody()));
//    }
//    @RabbitListener(queues = "queue_header02")
//    public void receiver08(Message msg){
//        log.info("queue02接收对象:"+msg);
//        log.info("queue02接收消息:"+new String(msg.getBody()));
//    }
}
